package za.org.jcicada.katcp;

/**
 * SensorLru.java
 *
 * @author Adrianna Pinska
 *
 * Created on October 27, 2008, 18:21
 */

import za.org.jcicada.katcp.ISensor;
import za.org.jcicada.katcp.ISensorValue;
import za.org.jcicada.katcp.SensorBase;
import za.org.jcicada.katcp.SensorValueBase;

/**
 * A base class for KAT device Line Replaceable Unit (LRU) sensors.
 */
public class SensorLru extends SensorBase {

    /**
     * LRU values.
     */
    public enum LruType {
        /** LRU is in a nominal state. */ NOMINAL,
        /** LRU is in an error state. */ ERROR;

        /**
         * Return the KATCP name of the LRU value.
         *
         * @return  KATCP name for the LRU value constant.
         */
        public String katcpName() {
            return name().toLowerCase();
        }

        /**
         * Find a LRU value for the given KATCP name.
         *
         * @param name  The KATCP name to lookup.
         * @return  The LRU constant corresponding to the given KATCP name.
         */
        public static LruType fromKatcpName(String name) {
            LruType value;
            try {
                value = LruType.valueOf(LruType.class, name.toUpperCase());
            }
            catch (IllegalArgumentException e) {
                throw new IllegalArgumentException(String.format("Unknown sensor LRU value name '%s'", name));
            }
            return value;
        }
    }

    /**
     * Value class for LRU sensors.
     */
    static public class LruValue extends SensorValueBase {

        /**
         * This LRU sensor's value.
         */
        protected LruType value;

        /**
         * Constructs an LruValue object.
         */
        public LruValue(){
            super();
            value = LruType.NOMINAL;
        }

        /**
         * Constructs an LruValue object.
         *
         * @param timestamp  When the sensor value was recorded.
         * @param status     The sensor status when the value was recorded.
         * @param value      The sensor value.
         */
        public LruValue(long timestamp, ISensorValue.Status status, LruType value){
            super(timestamp, status);
            this.value = value;
        }

        /**
         * Return the value part of the sensor value.
         *
         * @return The value part of the sensor value.
         */
        public LruType getValue() {
            return value;
        }
    }

    /**
     * Constructs a SensorLru object.
     *
     * @param name         The sensor name.
     * @param description  A description of the sensor.
     * @param units        The units of the sensor value.
     */
    public SensorLru(String name, String description, String units) {
        super(name, ISensor.Type.LRU, new LruValue(), description, units, new String [0]);
    } 

    // ISensor Methods

    /**
     * Format an LRU sensor value.
     *
     * @param value  Sensor value to format.
     * @return  The value formatted as a string.
     */
    public String formatValue(ISensorValue value) {
        LruType lvalue = ((LruValue) value).getValue();
        return lvalue.katcpName();
    }

}
